package e.odbo.data.generate;

import com.openbravo.data.loader.DBSession;
import e.odbo.data.generate.code.CodeGenerator;
import e.odbo.data.generate.db.DBGenerator;
import e.odbo.data.generate.model.I_ModelManager;
import e.odbo.data.generate.model.jumpmind.JumpmindManager;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.platform.IDatabasePlatform;

import java.io.*;
import java.util.HashMap;

public class GenerateManager {
    public HashMap<String, I_ModelManager> modelManager = new HashMap<>();

    private CodeGenerator codeGenerator;

    private DBGenerator dbGenerator;

    private GenerateConfig config;

    public GenerateManager(GenerateConfig config) {
        this.config = config;
        this.dbGenerator = new DBGenerator(config);
        this.codeGenerator = new CodeGenerator(config);
        modelManager.put(JumpmindManager.NAME, new JumpmindManager());
    }

    //模型读写
    public Database readDatabaseForFile(String fileNameWithRealPath) {
        I_ModelManager modelManager = getModelManager(fileNameWithRealPath);
        File modelFile = new File(fileNameWithRealPath);
        if (!modelFile.exists())
            return null;//throw
        Database createDatabase = null;
        try {
            InputStream in = new FileInputStream(modelFile);
            createDatabase = modelManager.readDatabase(in);
            in.close();
        } catch (IOException e) {
            System.out.print(e);
        }
        return createDatabase;
    }

    public void writeDatabaseToFile(Database database, String fileNameWithRealPath) {
        I_ModelManager modelManager = getModelManager(fileNameWithRealPath);
        File modelFile = new File(fileNameWithRealPath);
        try {
            if (modelFile.exists())
                modelFile.delete();
            modelFile.createNewFile();
            OutputStream out = new FileOutputStream(modelFile);
            modelManager.writeDatabase(database, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            System.out.print(e);
        }
    }

    private I_ModelManager getModelManager(String fileName) {
        String postfix = fileName.substring(fileName.lastIndexOf('.'));
        for (I_ModelManager modelManager : this.modelManager.values()) {
            if (modelManager.getSupportFilePostfix().equalsIgnoreCase(postfix))
                return modelManager;
        }
        return null;
    }


    //数据库相关
    public String generateDataBaseSQL(Database database, String dbDialect) {
        IDatabasePlatform configDatabasePlatform = getDataBasePlatformForDbDialect(dbDialect);
        dbGenerator.setConfigDatabasePlatform(configDatabasePlatform);
        return dbGenerator.generateDataBaseSQL(database, dbDialect, true);
    }

    public String generateDataBaseSQLForVersion(Database v1, Database v2, String dbDialect) {
        IDatabasePlatform configDatabasePlatform = getDataBasePlatformForDbDialect(dbDialect);
        dbGenerator.setConfigDatabasePlatform(configDatabasePlatform);
        return dbGenerator.generateDataBaseSQLForVersion(v1, v2);
    }

    public Database generateDatabaseForDB(DBSession session) {
        IDatabasePlatform configDatabasePlatform = getDataBasePlatformForDbDialect(session.getPlatform());
        dbGenerator.setConfigDatabasePlatform(configDatabasePlatform);
        return dbGenerator.generateDatabaseForDB();
    }

    public void generateDBSchema(Database database, DBSession session) {
        IDatabasePlatform configDatabasePlatform = getDataBasePlatformForDbDialect(session.getPlatform());
        dbGenerator.setConfigDatabasePlatform(configDatabasePlatform);
        dbGenerator.generateDBSchema(database, true, true);
    }

    public void updateDBSchema(Database database, DBSession session) {
        IDatabasePlatform configDatabasePlatform = getDataBasePlatformForDbDialect(session.getPlatform());
        dbGenerator.setConfigDatabasePlatform(configDatabasePlatform);
        dbGenerator.updateDBSchema(database, null, true);
    }


    //代码生成
    public void generateForModel(Database database) {
        codeGenerator.generateForModel(database, "java");
    }


    public IDatabasePlatform getDataBasePlatformForDbDialect(String dbDialect) {
        return null;
    }


}
